/**  
 * @Title: DetailController.java
 * @Package com.limit.control.site
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-11 上午10:25:26
 * @version V1.0
 */ 
package com.limit.control.site;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.ehcache.CacheKit;
import com.limit.constant.Constant;
import com.limit.model.Game;
import com.limit.model.GameComment;
import com.limit.model.GameType;


/** 
 * <br/>
 * @author maoyun(maoyun0903@163.com)
 * @date 2014-4-11 上午10:25:26 
 * @version V1.0
 */

public class DetailController extends BaseSiteController {

	/**
	 * 游戏详细资料 
	 */
	public void index(){
		long gid = this.getParaToLong("gid", 0L);
		Game game = Game.dao.findById(gid);
		if(game == null){
			this.render("500.html");
			return ;
		}
		long click_num = game.getLong("click_num");
		if(click_num < 0)	click_num = 0;
		game.set("click_num", ++click_num).update();
		this.setAttr("game", game);
		this.setAttr("action", this);
		this.render("detail.html");
	}
	
	/**
	 * 从缓存中得到游戏类型
	 */
	public String getType(long typeId){
		String name = new String();
		List<GameType> gameTypes = GameType.dao.getList();
		for (GameType gameType : gameTypes) {
			if(gameType.getLong("id") == typeId){
				name = gameType.getStr("name");
				break;
			}
		}
		return name;
	}
	
	/**
	 * 取得游戏相关评论列表
	 */
	public List<GameComment> getComments(long gid){
		List<GameComment> list = GameComment.dao.paginateList(gid ,1, pageSize).getList();
		return list;
	}
	
	
	/**
	 * 评论
	 */
	public void commitComment(){
		
		long gid = this.getParaToLong("gid",0L);
		String title = this.getPara("title", "");
		String content = this.getPara("content","");
		if(gid <= 0 || title.equals("") || content.equals("")){
			render("500.html");
			return ;
		}
		GameComment.dao.set("game_id", gid)
						.set("title", title)
						.set("content", content)
						.set("create_time", new Date())
						.set("ip", this.getRequest().getRemoteHost())
						.set("useful", 0).set("unuseful", 0)
						.set("grade", 0.0).set("is_check", 1).save();
		
		this.redirect("/detail/index?gid="+gid , true);
	}
}
